﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FF.Entity;
using System.Data.SQLite;
using System.Windows.Forms;

namespace FF.Method
{
    class CustomerTypeMethod
    {
        public static int addType(CustomerType userType)
        {
            string sql = "insert into ff_customer_type values('";
            sql += userType.type + "','";
            sql += userType.name + "','";
            sql += userType.remark + "','";
            sql += userType.discount + "')";
            return SQLiteHelper.ExecuteSql(sql);
        }
        public static CustomerType getTypeByTypeCode(string typeCode)
        {
            CustomerType type = null;
            try
            {
                SQLiteDataReader reader = SQLiteHelper.ExecuteReader("select * from ff_customer_type where type='" + typeCode + "'");
                if (reader.HasRows)
                {
                    reader.Read();
                    type = new CustomerType();
                    type.type = reader.GetString(0);
                    type.name = reader.GetString(1);
                    type.remark = reader.GetString(2);
                    type.discount = reader.GetString(3);
                }
                reader.Close();
                return type;
            }
            catch (Exception ex)
            {
                MessageBox.Show("程序出错了！" + ex.Message, "提示信息 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }
        }
        public static int updateType(CustomerType ut)
        {
            string sql = "update ff_customer_type set ";
            //sql += "user_id='"+user.UserId;
            sql += "name='" + ut.name;
            sql += "',remark='" + ut.remark;
            sql += "',discount='" + ut.discount;
            sql += "' where type='" + ut.type + "'";
            return SQLiteHelper.ExecuteSql(sql);
        }
        public static int deleteType(string type)
        {
            string sql = "delete from ff_customer_type where type='" + type + "'";
            return SQLiteHelper.ExecuteSql(sql);
        }

        public static string getDiscountByType(string type)
        {
            try
            {
                string discount = "";
                SQLiteDataReader reader = SQLiteHelper.ExecuteReader("select discount from ff_customer_type where type='" + type + "'");
                if (reader.HasRows)
                {
                    reader.Read();
                    //MessageBox.Show(reader.GetString(0).ToString());
                    discount = (reader.GetString(0) == null ? "" : reader.GetString(0));
                }
                reader.Close();
                return discount;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("程序出错了！" + ex.Message, "提示信息 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return "";
            }
        }
    }
}
